﻿using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using MyFloridaHouse.DataAccess.Extensions;
using MyFloridaHouse.libSvDTO;

//TODO: Secure all function here so an exception doesn't kill the program
namespace MyFloridaHouse.DataAccess
{
	/// <summary>
	/// Data access layer storage operation methods.
	/// </summary>
    public partial class DAL_CensusData 
    {
		/// <summary>
		/// Initializes a new instance of the <see cref="DAL_CensusData"/> class.
		/// Data access layer storage operation methods.
		/// </summary>
        public DAL_CensusData()
        {
        }

        /// <summary>
        /// Establishes the database connection
        /// </summary>
        /// <returns>SqlConnection</returns>
        /// <remarks>None</remarks>
        internal static SqlConnection Connect()
        {
            ConnectionStringSettingsCollection connectionStrings = ConfigurationManager.ConnectionStrings;

            ConnectionStringSettings conStrSetting = connectionStrings["MyFloridaHouse.MapServer.Properties.Settings.Reap"];

            string conStr = conStrSetting.ToString();
            SqlConnection sqlConn = new SqlConnection(conStr);
            return sqlConn;
        }

		/// <summary>
		/// Gets the layers.
		/// </summary>
		/// <returns>list of <see cref="LayerDTO"/></returns>
		/// <remarks>SPROC: usp_moGetLayers</remarks>
        public List<LayerDTO> GetLayers()
        {
            List<LayerDTO> itemList = new List<LayerDTO>();

            //string storedProcedureName = "usp_moGetLayers";
            string storedProcedureName = "usp_tl10GetLayers10";

            SqlConnection db = Connect();
            SqlCommand StoredProcedureCommand = new SqlCommand(storedProcedureName, db);
            StoredProcedureCommand.CommandType = CommandType.StoredProcedure;
            db.Open();
            using (SqlDataReader rdr = StoredProcedureCommand.ExecuteReader())
            {
                while (rdr.Read())
                {
                    itemList.Add(LayerDTODataContract.DataContract(rdr));
                }
            }
            return itemList;
        }

        //Todo: Remove unused code.
		#region Unused Code.

		/// <summary>
		/// [future] Gets the data dictionary denominators.
		/// </summary>
		/// <param name="iDataPartIDList">The i data part ID list.</param>
		/// <returns></returns>
		/// <remarks>[future] SPROC: usp_GetDataDictionaryDenominators</remarks>
		public List<DataDictionaryDenominatorDTO> GetDataDictionaryDenominators(string iDataPartIDList)
        {
            List<DataDictionaryDenominatorDTO> itemList = new List<DataDictionaryDenominatorDTO>();

            //string storedProcedureName = "usp_GetDataDictionaryDenominators";

            //using (SqlConnection db = Connect())
            //{
            //    SqlCommand StoredProcedureCommand = new SqlCommand(storedProcedureName, db);
            //    StoredProcedureCommand.CommandType = CommandType.StoredProcedure;
            //    SqlParameter dataPartIDParm = StoredProcedureCommand.Parameters.AddWithValue("iDataPartIDList", iDataPartIDList);
            //    dataPartIDParm.Direction = ParameterDirection.Input;

            //    db.Open();
            //    using (SqlDataReader rdr = StoredProcedureCommand.ExecuteReader())
            //    {
            //        while (rdr.Read())
            //        {
            //            itemList.Add(DataDictionaryDenominatorDTODataContract.DataContract(rdr));
            //        }
            //    }
            //}
            return itemList;
        }

		/// <summary>
		/// [future] Gets the data dictionary parts.
		/// </summary>
		/// <returns></returns>
		/// <remarks>[future] SPROC: usp_GetDataDictionaryParts</remarks>
        public List<DataDictionaryPartDTO> GetDataDictionaryParts()
        {
            List<DataDictionaryPartDTO> itemList = new List<DataDictionaryPartDTO>();

            //Database db = Connect();
            //DbCommand dbCommand = db.GetStoredProcCommand("usp_GetDataDictionaryParts");
            //using (IDataReader rdr = db.ExecuteReader(dbCommand))
            //{
            //    while (rdr.Read())
            //    {
            //        itemList.Add(DataDictionaryPartDTODataContract.DataContract(rdr));
            //    }
            //}
            return itemList;
        }

		/// <summary>
		/// [future] Gets the layer field data.
		/// </summary>
		/// <param name="iLayerID">The i layer ID.</param>
		/// <returns></returns>
        /// <remarks>[future] SPROC: usp_GetLayerFieldData</remarks>
		public List<LayerFieldDataDTO> GetLayerFieldData(int iLayerID)
        {
            List<LayerFieldDataDTO> itemList = new List<LayerFieldDataDTO>();

            //Database db = Connect();
            //DbCommand dbCommand = db.GetStoredProcCommand("usp_GetLayerFieldData");
            //db.AddInParameter(dbCommand, "iLayerID", DbType.String, iLayerID);
            //using (IDataReader rdr = db.ExecuteReader(dbCommand))
            //{
            //    while (rdr.Read())
            //    {
            //        itemList.Add(LayerFieldDataDTODataContract.DataContract(rdr));
            //    }
            //}
            return itemList;
		}

        //public List<LayerDataMinMaxDTO> GetMinMaxTotals(int iLayerID, string iDictionaryIDList)
        //{
        //    List<LayerDataMinMaxDTO> itemList = new List<LayerDataMinMaxDTO>();

        //    Database db = DatabaseFactory.CreateDatabase("Reap");
        //    DbCommand dbCommand = db.GetStoredProcCommand("usp_GetMinMaxTotals");
        //    db.AddInParameter(dbCommand, "iLayerID", DbType.String, iLayerID);
        //    db.AddInParameter(dbCommand, "iDictionaryIDList", DbType.String, iDictionaryIDList);
        //    using (IDataReader rdr = db.ExecuteReader(dbCommand))
        //    {
        //        while (rdr.Read())
        //        {
        //            itemList.Add(LayerDataMinMaxDTODataContract.DataContract(rdr));
        //        }
        //    }
        //    return itemList;
        //}

		#endregion
	}
}
